<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="教师"  prop="teacherId">

              <a-select v-model="model.teacherId" placeholder="请选择任课教师"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in teacherList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
<!--              <s-dict-select-tag type='radioButtonNew' placeholder='请选择教师' v-model='model.teacherId' -->
<!--                                 dictCode='tea_teacher,teacher_name,id,teacher_iz_open =1 and del_flag =0  order by teacher_order'></s-dict-select-tag>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item  abel="可任教科目"  prop="subjectId">
              <s-multi-select-tag type='rediobutton'  v-model='model.subjectId' placeholder='请选择科目' is-pin-yin="true"
                                  dictCode='cou_subject,subject_name,id,subject_iz_open =1 and del_flag =0  order by subject_order' />
<!--              <a-select v-model="model.subjectId" placeholder="请选择任教科目"-->
<!--                        :getPopupContainer="(target) => target.parentNode">-->
<!--                <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>-->
<!--              </a-select>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任教关系状态" prop="relStatus">
              <s-dict-select-tag type='radioButtonNew' placeholder='请选择状态' v-model='model.relStatus'      dictCode='rel_status'></s-dict-select-tag>

            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getTeacherList, getSubjectList } from '@/api/commonAPI'

export default {
  name: 'RelTeacherSubjectForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/rel_teacher_subject/relTeacherSubject/add',
        edit: '/rel_teacher_subject/relTeacherSubject/edit',
        queryById: '/rel_teacher_subject/relTeacherSubject/queryById'
      },
      teacherList: [],
      subjectList: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initSubjectList()
    this.initTeacherList()
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        let tenantId = localData.value.tenantId
        this.model.tenantId = tenantId
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    async initTeacherList() {
      let res = await getTeacherList()
      this.teacherList = []
      res.result.forEach(item => {
        this.teacherList.push({
          label: item.teacherName,
          value: item.teacherId
        })
      })
    },
    async initSubjectList() {
      let res = await getSubjectList()
      this.subjectList = []
      res.result.forEach(item => {
        this.subjectList.push({
          label: item.subjectName,
          value: item.subjectId
        })
      })
    }
  }
}
</script>